package com.example.smartcity_ikun3.ui.dashboard

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.smartcity_ikun3.R
import com.example.smartcity_ikun3.logic.base.BaseFragment
import com.example.smartcity_ikun3.ui.App.Companion.url
import com.example.smartcity_ikun3.ui.home.HomeFragment
import com.example.smartcity_ikun3.ui.home.HomeViewModel
import com.example.smartcity_ikun3.ui.home.adapter.ReAdapterMore
import com.example.smartcity_ikun3.ui.home.model.MoreEntity

class DashboardFragment : BaseFragment() {

    private val viewModel by lazy {
        ViewModelProviders.of(this).get(HomeViewModel::class.java)
    }


    private lateinit var mMoreRecyclerView: RecyclerView

    private lateinit var root: View
    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        root = inflater.inflate(R.layout.fragment_dashboard, container, false)
        mMoreRecyclerView = root.findViewById(R.id.MoreRecyclerView)


        val array = ArrayList<MoreEntity>()

        viewModel.setGetMore(1)
        viewModel.getMoreLiveData.observe(this, Observer {
            if (it.code == 200){
                viewModel.getMoreList.clear()
                viewModel.getMoreList.addAll(it.data!!)
            }

            for (i in viewModel.getMoreList){
                if (array.size < viewModel.getMoreList.size){
                    array.add(MoreEntity(url + i.imgUrl.toString(), i.liveName.toString()))
                    mMoreRecyclerView.adapter?.notifyItemChanged(array.size)
                }
            }
        })


        mMoreRecyclerView.apply {
            this.layoutManager = GridLayoutManager(context, 4)
            this.adapter = ReAdapterMore(array)
        }

        return root
    }

    override fun getLayout(): Int {
        return R.layout.fragment_dashboard
    }

    override fun initData() {

    }

    override fun initView() {

    }

    override fun initEvent() {

    }
}